import 'package:dio/dio.dart';
import 'dart:io';

class UploadApi {
  /// 上传单张图片
  static Future<String> uploadImage(File file) async {
    // 创建 FormData
    String fileName = file.path.split('/').last;
    // 后端接收文件的参数名为 file
    FormData formData = FormData.fromMap({
      "file": await MultipartFile.fromFile(
        file.path,
        filename: fileName,
      ),
    });

    // TODO 发送请求

    // 假设接口返回图片URL
    return '';
  }

  /// 上传多张图片
  static Future<List<String>> uploadImages(List<File> files) async {
    // 构建多文件上传
    var formList = [];
    for (var file in files) {
      String fileName = file.path.split('/').last;
      formList.add(await MultipartFile.fromFile(
        file.path,
        filename: fileName,
      ));
    }

    FormData formData = FormData.fromMap({
      "file": formList,  // 后端接收多个文件的参数名仍为 file
    });

    // TODO 发送请求

    // 假设接口返回图片URL列表
    List<String> urls = [];

    return urls;
  }
}